home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DDJMAG / DDJ9310.ZIP / DFPP03.ZIP / TIMER.CPP < prev    next >
C/C++ Source or Header  |  1993-08-15  |  1KB  |  50 lines

  1. // -------- timer.cpp
  2.  
  3. #include <dos.h>
  4. #include "timer.h"
  5.  
  6. void interrupt (*Timer::OldTimer)(...);
  7.  
  8. Timer *Timer::timers[MAXTIMERS];
  9. int Timer::timerct = 0;
  10.  
  11. // ------- timer interrupt service routine
  12. void interrupt NewTimer(...)
  13. {
  14.     // --- countdown all running timers
  15.     for (int i = 0; i < MAXTIMERS; i++)
  16.         if (Timer::timers[i] != 0)
  17.             if (Timer::timers[i]->TimerRunning())
  18.                 Timer::timers[i]->Countdown();
  19.     // --- chain to the old interrupt vector
  20.     (*Timer::OldTimer)();
  21. }
  22.  
  23. Timer::Timer()
  24. {
  25.     timer = -1;
  26.     // --- if first timer, hook and chain the interrupt vector
  27.     if (timerct == 0)    {
  28.         OldTimer = getvect(TIMER);
  29.         setvect(TIMER, NewTimer);
  30.     }
  31.     // --- add the timer to the table
  32.     timers[timerct++] = this;
  33. }
  34.  
  35. Timer::~Timer()
  36. {
  37.     // --- remove the timer from the table
  38.     for (int i = 0; i < MAXTIMERS; i++)    {
  39.         if (this == timers[i])    {
  40.             timers[i] = 0;
  41.             break;
  42.         }
  43.     }
  44.     // --- if last one, restore the interrupt vector
  45.     if (--timerct == 0)
  46.         setvect(TIMER, OldTimer);
  47. }
  48.  
  49.  
  50.